<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Member extends Allow
{
    public function getindex()
    {
        //获取member表用户信息
        $member=Db::table("member")->select();
        // $member.length
        for($i=0;$i<count($member);$i++){
            $member[$i]['password']='**********';
        }
        // var_dump($member);exit;
    	//加载后台用户模板
    	return $this->fetch("Member/index",['member'=>$member]);
    }
     //添加
    public function getadd(){
        //获取添加页面
        return $this->fetch("Member/add");
    }
    //执行添加
    public function postdoadd(){
        //创建请求
        $request = request();
        $data = $request->only(['tel','password','member']);
        $preg1="/^1[34578]\d{9}$/";
        $preg2="/^\w{6,8}$/";
        if(preg_match($preg1,$data['tel'],$arr1) && preg_match($preg2,$data['password'],$arr2)){
            if(Db::table('member')->insert($data)){
                $this->success("添加成功","/adminmember/index");
            }else{
                $this->error("添加失败");
            }
        }else{
            $this->error("手机号或密码格式不正确");
        }
        
    }

    //删除
    public function getdelete(){
        $request=request();
        //获取id
        $id=$request->param('id');
        if(Db::table('member')->where('id',"{$id}")->delete()){
            $this->success("删除成功","/adminmember/index");
        }else{
            $this->error("删除失败","/adminmember/index");
        }
    }

    //修改
    public function getedit(){
        $request=request();
        //获取id
        $id=$request->param('id');
        //获取用户信息
        $info=Db::table('member')->where('id',"{$id}")->find();
        // var_dump($info);exit;
        //获取添加页面
        return $this->fetch("Member/update",['info'=>$info]);
    }
    //执行修改
    public function postupdate(){
        //创建请求
        $request = request();
        //获取用户id
        $id=$request->param('id');
        $data = $request->only(['tel','password','member']);
        $preg1="/^1[34578]\d{9}$/";
        $preg2="/^\w{6,8}$/";
        if(preg_match($preg1,$data['tel'],$arr1) && preg_match($preg2,$data['password'],$arr2)){
            if(Db::table('member')->where('id',"{$id}")->update($data)){
                $this->success("更新成功","/adminmember/index");
            }else{
                $this->error("更新失败");
            }
        }else{
            $this->error("手机号或密码格式不正确","/adminmember/edit/id/{$id}");
        }
        // var_dump($tel);exit;
        
    }

}
